unit tasacschart;

interface

uses pgchartunit, extctrls, controls, pganalysistypesunit, apgabstractunit,
   classes, chartaxisunit, pgseriesunit, seriesplot, ifmgdi, sysutils,
   ifmmemoryunit, db, apgabstracttypesunit, nansischart, xtasacs, index;

const vPopulation = 0;
const vMortality = 1;

type
   TAgeYearChart = class(PgChartWithSeriesPlot)
   private
      m_Variable : Integer;
   protected
   public
      function CreateChartAxis(Axis : PgAxisType) : APgChartAxis; override;
      function CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries; override;
      constructor Create(_Parent : TPanel; Variable : Integer; _TextZoom : double = 0.85); reintroduce;
      property Variable : Integer read m_Variable;
   end; // TAgeYearChart

   AgeAxis = class(PgChartAxis)
   protected
      function GetNumDimensionElms : Integer; override;
      function GetAxisLabel : string; override;
      function GetFixedInterval : Integer; override;
   end; // AgeAxis


   AgeAxisSeries = class(PgSeries)
   protected
      function GetIntervalName(I : Integer) : string; override;
   end; // AgeAxisSeries


   YearAxis = class(PgChartAxis)
   protected
      function GetNumDimensionElms : Integer; override;
      function GetAxisLabel : string; override;
      function GetFixedInterval : Integer; override;
   end; // YearAxis

   YearAxisSeries = class(PgSeries)
   protected
      function GetIntervalName(I : Integer) : string; override;
   end; // YearAxisSeries


   YearAgeSeries = class(PgSeries)
   protected
      function GetShowZeros : Boolean; override;
      function GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double; override;
   end; // YearAgeSeries



implementation


///////////////////////
// TSimpleChart


constructor TAgeYearChart.Create(_Parent : TPanel; Variable : Integer; _TextZoom : double);
var
   m : APgMetaSeries;
begin
   inherited Create(_Parent, _TextZoom);
   m_Variable := Variable;
   ShowSeriesLegend := false;
   ShowMeanLegend := false;
   ShowZscalelegend := true;
   PlotType := ptCategoryMultiple;

   chartaxis[atx].ContinuousGroupData := false;
   chartaxis[atx].AxisLabel := 'Age';
   chartaxis[aty].AxisLabel := 'Year';
   chartaxis[aty].DefaultAxisLabel := false;
   chartaxis[aty].ShowMajorGrid := false;
//   chartaxis[aty].DefaultMin := false;
//   chartaxis[aty].DefaultMax := false;
//   chartaxis[aty].MinValue := 0;
//  chartaxis[aty].ShowChartAxis := false;
   chartaxis[aty].ContinuousGroupData := false;
   ChartAxis[atZ].DefaultMin := true;
   ChartAxis[atZ].DefaultMax := true;
//   ChartAxis[atZ].LnTransformed := true;
//   chartaxis[aty].DefaultMinorMajor := false;
//   chartaxis[aty].NumMinorPerMajor := 1;
//   chartaxis[aty].NumDecimals := 0;
   // Setup meta series at y axis:
   with ChartAxis[atZ].MetaSeries do
   begin
      m := PgMetaSeries.Create(ChartAxis[aty], 'Number', 0);
      m.BarPlot := true;
      m.LinePlot := false;
      m.Scatter := false;
      Add(Pointer(m));
   end; // with
end; // TAgeYearChart.Create


function TAgeYearChart.CreateChartAxis(Axis : PgAxisType) : APgChartAxis;
begin
   case Axis of
      atx:
         Result := AgeAxis.Create(Self as IChart, Axis);
      aty:
      begin
         Result := YearAxis.Create(Self as IChart, Axis);
         Result.GapWidth := 40;
      end;
   else
      Result := PgChartAxis.Create(Self as IChart, Axis);
   end; // case
end; // TAgeYearChart.CreateChartAxis


function TAgeYearChart.CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries;
begin
   Result := nil;
   case PrimaryChartAxis.axis of
      atX:
         if Assigned(MetaSeries) then
            Result := nil
         else
            Result := AgeAxisSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex);
      atY:
         if Assigned(MetaSeries) then
            Result := YearAgeSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex)
         else
            Result := YearAxisSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex);
      atZ:
   end; // case
end; // TAgeYearChart.CreateSeries



////////////////////////////
// AgeAsis


function AgeAxis.GetAxisLabel : string;
begin
      if DefaultAxisLabel then
         Result := 'age'
      else
         Result := inherited GetAxisLabel;
end; // AgeAxis.GetAxisLabel


(*const c_NumXValues = 10;
var x_val : array[1..c_NumXValues] of integer = (20, 21, 22, 23, 24, 25, 26, 27, 28, 29);
var y_val : array[1..c_NumXValues] of integer = (0, 1, 3, 7, 14, 25, 7, 3, 1, 0);*)


function AgeAxis.GetNumDimensionElms : Integer;
begin
   Result := NumAges;
end; // AgeAxis.GetNumDimensionElms


function AgeAxis.GetFixedInterval : Integer;
begin
   Result := 1; // x axis category interval
end; // AgeAxis.GetFixedInterval


/////////////////////////
// AgeAxisSeries


function AgeAxisSeries.GetIntervalName(I : Integer) : string;
begin
   Result := inttostr(I + round(get_Run_Prop(propFirstAge)));
end; // AgeAxisSeries.GetInternalValue


////////////////////////////
// YearAxis


function YearAxis.GetAxisLabel : string;
begin
   if DefaultAxisLabel then
      Result := 'year'
   else
      Result := inherited GetAxisLabel;
end; // YearAxis.GetAxisLabel


function YearAxis.GetNumDimensionElms : Integer;
begin
   Result := NumYears;
end; // YearAxis.GetNumDimensionElms


function YearAxis.GetFixedInterval : Integer;
begin
   Result := 1;
end; // YearAxis.GetFixedInterval


/////////////////////////
// YearAxisSeries


function YearAxisSeries.GetIntervalName(I : Integer) : string;
begin
   Result := inttostr(I + round(get_Run_Prop(propFirstYear)));
end; // YearAxisSeries.GetInternalName


///////////////////////////////
// YearAgeSeries


function YearAgeSeries.GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double;
var
   iAge, iYear  : Integer;
begin
   case SeriesVariable of
      svIntervalDelta:
         begin
            iYear := I + PrimaryChartAxis.Series.MinCategory;
            iAge := XIndex + PrimaryChartAxis.Chart.CHartAxis[atx].Series.MinCategory;
            case(TAgeYearChart((PrimaryChartAxis.Chart as IInterfaceComponentReference).GetComponent).Variable) of
               vPopulation:
                  Result := get_N(iYear, iAge);
               vMortality:
                  Result := get_F(iYear, iAge);
            end; //case
         end;
   else
      Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries)
   end; // case
end; // YearAgeSeries.GetInternalValue


function YearAgeSeries.GetShowZeros : Boolean;
begin
   Result := false;
end; // YearAgeSeries.GetShowZeros


end.
